package org.eae.eldf.service.base.impl;

import java.util.Date;
import java.util.List;

import org.eae.eldf.dao.base.IBaseCompanyLinkmanDao;
import org.eae.eldf.dao.base.impl.BaseCompanyLinkmanDaoImpl;
import org.eae.eldf.entity.base.BaseCompanyLinkman;
import org.eae.eldf.framework.exception.ServiceException;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.service.BaseServiceImpl;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.base.BaseCompanyLinkmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class BaseCompanyLinkmanServiceImpl extends BaseServiceImpl<BaseCompanyLinkman> implements
		BaseCompanyLinkmanService {
	private IBaseCompanyLinkmanDao baseCompanyLinkmanDao;

	@Autowired
	public void setBaseCompanyLinkmanDao(BaseCompanyLinkmanDaoImpl baseCompanyLinkmanDao) {
		this.baseCompanyLinkmanDao = baseCompanyLinkmanDao;
	}

	@Override
	public Pagination<BaseCompanyLinkman> pageLinkmanList(Pagination<BaseCompanyLinkman> pager, String searchValue) {
		// TODO Auto-generated method stub
		pager = baseCompanyLinkmanDao.pageLinkmanList(pager, searchValue);
		return pager;
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		baseCompanyLinkmanDao.update(Criteria.create(BaseCompanyLinkman.class).set("valid", 0)
				.where("baseCompanyLinkmanId", new Object[] { id }));
	}

	@Override
	public BaseCompanyLinkman findById(Long id) {
		// TODO Auto-generated method stub
		return baseCompanyLinkmanDao.get(BaseCompanyLinkman.class, id);
	}

	@Override
	public Long insertBaseCompanyLinkman(BaseCompanyLinkman baseCompanyLinkman) throws ServiceException {
		// TODO Auto-generated method stub
		if (baseCompanyLinkman.getBaseCompanyLinkmanId() == null) {
//			baseCompanyLinkman.setValid(1l);

			baseCompanyLinkman.setCreateTime(new Date());
			baseCompanyLinkman.setLastUpdateTime(new Date());
			baseCompanyLinkman.setCreateUser(SpringSecurityUtils.getCurrentUserName());
			baseCompanyLinkman.setLastUpdateUser(SpringSecurityUtils.getCurrentUserName());

			return baseCompanyLinkmanDao.insert(baseCompanyLinkman);
		} else {
			throw new ServiceException(" error code待定.");
		}
	}

	@Override
	public void updateBaseCompanyLinkman(BaseCompanyLinkman baseCompanyLinkman) throws ServiceException {
		// TODO Auto-generated method stub
		if (baseCompanyLinkman.getBaseCompanyLinkmanId() != null) {

			baseCompanyLinkman.setLastUpdateTime(new Date());
			baseCompanyLinkman.setLastUpdateUser(SpringSecurityUtils.getCurrentUserName());

			baseCompanyLinkmanDao.update(baseCompanyLinkman);
		} else {
			throw new ServiceException(" error code待定.");
		}
	}

	@Override
	public List<BaseCompanyLinkman> findLinkmanByCompanyCode(String baseCompanyCode) {
		// TODO Auto-generated method stub
		return baseCompanyLinkmanDao.findLinkmanByCompanyCode(baseCompanyCode);
	}

}
